/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.util.DateUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.lmtpserver.LmtpCallback;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import javax.mail.internet.MailDateFormat;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class QuotaWarning
/*     */   implements LmtpCallback
/*     */ {
/*  44 */   private static final QuotaWarning sInstance = new QuotaWarning();
/*     */   
/*     */ 
/*     */ 
/*     */   public static QuotaWarning getInstance()
/*     */   {
/*  50 */     return sInstance;
/*     */   }
/*     */   
/*     */   public void afterDelivery(Account account, Mailbox mbox, String envelopeSender, String recipientEmail, Message newMessage)
/*     */   {
/*     */     try
/*     */     {
/*  57 */       int warnPercent = account.getIntAttr("zimbraQuotaWarnPercent", 90);
/*  58 */       long quota = AccountUtil.getEffectiveQuota(account);
/*  59 */       long warnInterval = account.getTimeInterval("zimbraQuotaWarnInterval", 86400000L);
/*  60 */       String template = account.getAttr("zimbraQuotaWarnMessage", null);
/*     */       
/*  62 */       Date lastWarnTime = account.getGeneralizedTimeAttr("zimbraQuotaLastWarnTime", null);
/*  63 */       Date now = new Date();
/*     */       
/*  65 */       ZimbraLog.lmtp.debug("Checking quota warning: mbox size=%d, quota=%d, lastWarnTime=%s, warnInterval=%d, warnPercent=%d", new Object[] { Long.valueOf(mbox.getSize()), Long.valueOf(quota), lastWarnTime, Long.valueOf(warnInterval), Integer.valueOf(warnPercent) });
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*  70 */       if ((quota == 0L) || (warnPercent == 0)) {
/*  71 */         return;
/*     */       }
/*  73 */       if (mbox.getSize() * 100L / quota < warnPercent) {
/*  74 */         return;
/*     */       }
/*  76 */       if ((lastWarnTime != null) && (now.getTime() - lastWarnTime.getTime() < warnInterval)) {
/*  77 */         return;
/*     */       }
/*  79 */       if (template == null) {
/*  80 */         ZimbraLog.lmtp.warn("%s not specified.  Unable to send quota warning message.", new Object[] { "zimbraQuotaWarnMessage" });
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*  85 */       ZimbraLog.lmtp.info("Sending quota warning: mbox size=%d, quota=%d, lastWarnTime=%s, warnInterval=%dms, warnPercent=%d", new Object[] { Long.valueOf(mbox.getSize()), Long.valueOf(quota), lastWarnTime, Long.valueOf(warnInterval), Integer.valueOf(warnPercent) });
/*     */       
/*  87 */       String address = account.getAttr("zimbraMailDeliveryAddress");
/*  88 */       String domain = com.zimbra.common.util.EmailUtil.getLocalPartAndDomain(address)[1];
/*     */       
/*  90 */       Map<String, Object> vars = new HashMap();
/*  91 */       vars.put("RECIPIENT_NAME", account.getAttr("displayName"));
/*  92 */       vars.put("RECIPIENT_DOMAIN", domain);
/*  93 */       vars.put("RECIPIENT_ADDRESS", address);
/*  94 */       vars.put("DATE", new MailDateFormat().format(new Date()));
/*  95 */       vars.put("MBOX_SIZE_MB", String.format("%.2f", new Object[] { Double.valueOf(mbox.getSize() / 1024.0D / 1024.0D) }));
/*  96 */       vars.put("QUOTA_MB", String.format("%.2f", new Object[] { Double.valueOf(quota / 1024.0D / 1024.0D) }));
/*  97 */       vars.put("WARN_PERCENT", Integer.valueOf(warnPercent));
/*  98 */       vars.put("NEWLINE", "\r\n");
/*     */       
/* 100 */       String msgBody = StringUtil.fillTemplate(template, vars);
/* 101 */       ParsedMessage pm = new ParsedMessage(msgBody.getBytes(), Long.valueOf(now.getTime()), false);
/* 102 */       DeliveryOptions dopt = new DeliveryOptions().setFolderId(2).setFlags(Flag.BITMASK_UNREAD | Flag.BITMASK_HIGH_PRIORITY);
/* 103 */       mbox.addMessage(null, pm, dopt, null);
/*     */       
/*     */ 
/* 106 */       Map<String, String> attrs = new HashMap();
/* 107 */       attrs.put("zimbraQuotaLastWarnTime", DateUtil.toGeneralizedTime(now));
/* 108 */       Provisioning.getInstance().modifyAttrs(account, attrs);
/*     */     } catch (Exception e) {
/* 110 */       ZimbraLog.lmtp.warn("Unable to send quota warning message", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void forwardWithoutDelivery(Account account, Mailbox mbox, String envelopeSender, String recipientEmail, ParsedMessage pm) {}
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/QuotaWarning.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */